package com.itheima.utils;

import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

@Component
public class MinioUtil {

    public static String upLoadFile(MultipartFile file, String fileName) {
        String objectName = fileName == null ? file.getOriginalFilename() : fileName;
        try (InputStream inputStream = file.getInputStream()) {
            MinioClient minioClient = MinioClient.builder()
                    .endpoint("http://117.72.15.81:9000")
                    .credentials("minioadmin", "minioadmin")
                    .build();
            minioClient.putObject(
                    PutObjectArgs.builder()
                            .bucket("itheima")
                            .object(objectName)
                            .stream(inputStream, file.getSize(), -1)
                            .contentType(file.getContentType())
                            .build()
            );
        } catch (Exception e) {
            return "Error";
        }
        return "http://117.72.15.81:9000/itheima/" + objectName;
    }
}
